package com.mujin.keji.collectionsystem.module.login.views;

import android.Manifest;
import android.arch.lifecycle.LifecycleActivity;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;

import com.mujin.keji.collectionsystem.CcsApplication;
import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.module.login.dto.LoginDto;
import com.mujin.keji.collectionsystem.module.main.dto.QueryPhoneDto;
import com.mujin.keji.collectionsystem.module.main.viewmodel.MainViewModel;
import com.mujin.keji.collectionsystem.module.main.views.MainActivity;
import com.mujin.keji.collectionsystem.module.mypager.views.ValidationMyPhoneActivity;
import com.mujin.keji.collectionsystem.permissions.PermissionUtils;
import com.mujin.keji.collectionsystem.permissions.PermissionsObserver;
import com.mujin.keji.collectionsystem.utils.DialogUtils;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.mujin.keji.collectionsystem.utils.RxTimerUtil;
import com.mujin.keji.collectionsystem.utils.SPUtil;
import com.tbruyelle.rxpermissions.RxPermissions;

/**
 * 欢迎页界面
 *
 * @author weishixiong
 * @Time 2018-05-23
 */

public class WellcomeActivity extends LifecycleActivity {
    private MainViewModel mainViewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CcsApplication.addActivity(this);
        mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        RxPermissions.getInstance(this).request(
                Manifest.permission.RECORD_AUDIO,//录音权限
                Manifest.permission.ACCESS_COARSE_LOCATION, //定位权限
                Manifest.permission.ACCESS_FINE_LOCATION,//定位权限
                Manifest.permission.WRITE_EXTERNAL_STORAGE,//写权限
                Manifest.permission.READ_EXTERNAL_STORAGE, //读权限
                Manifest.permission.READ_PHONE_STATE,//电话权限
                Manifest.permission.CAMERA,//相机权限
                Manifest.permission.PROCESS_OUTGOING_CALLS//获取电话状态权限

        ).subscribe(new PermissionsObserver() {
            @Override
            protected void onGetPermissionsSuccess() {
                Boolean iscomin = SPUtil.getData(Constant.SP.CONSTANT, Constant.SP.ISCOMIN, Boolean.class, false);
                //获取本地登录的缓存
                LoginDto dto = SPUtil.getData(Constant.SP.SP, Constant.SP.LOGIN_KEY, LoginDto.class, null);
                if (iscomin) {
                    if (dto == null) {
                        startActivity(new Intent(WellcomeActivity.this, LoginActivity.class));
                    } else {
                        //如果登录有数据的话 直接去主页面
                        startActivity(new Intent(WellcomeActivity.this, MainActivity.class));
                    }
                    finish();
                } else {
                    RxTimerUtil.timer(500, new RxTimerUtil.IRxNext() {
                        @Override
                        public void doNext() {
                            startActivity(new Intent(WellcomeActivity.this, SplashActivity.class));
                            finish();
                        }
                    });

                }

            }

            @Override
            protected void onGetPermissionsSuccessFailuer() {
                if (!PermissionUtils.hasPermission(WellcomeActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION})) {
                    DialogUtils.showPerMissionDailog(Constant.FINALVALUE.LOCATION_MESSAGE, WellcomeActivity.this);
                    return;
                }
                if (!PermissionUtils.hasPermission(WellcomeActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION})) {
                    DialogUtils.showPerMissionDailog(Constant.FINALVALUE.LOCATION_MESSAGE, WellcomeActivity.this);
                    return;
                }

                if (!PermissionUtils.hasPermission(WellcomeActivity.this, new String[]{Manifest.permission.CAMERA})) {
                    DialogUtils.showPerMissionDailog(Constant.FINALVALUE.CAMERA_MESSAGE, WellcomeActivity.this);
                    return;
                }
                if (!PermissionUtils.hasPermission(WellcomeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE})) {
                    DialogUtils.showPerMissionDailog(Constant.FINALVALUE.EXTERNAL_MESSAGE, WellcomeActivity.this);
                    return;
                }
                if (!PermissionUtils.hasPermission(WellcomeActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE})) {
                    DialogUtils.showPerMissionDailog(Constant.FINALVALUE.EXTERNAL_MESSAGE, WellcomeActivity.this);
                    return;
                }
                if (!PermissionUtils.hasPermission(WellcomeActivity.this, new String[]{Manifest.permission.RECORD_AUDIO})) {
                    DialogUtils.showPerMissionDailog(Constant.FINALVALUE.RECORD_MESSAGE, WellcomeActivity.this);
                    return;
                }
                if (!PermissionUtils.hasPermission(WellcomeActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE})) {
                    DialogUtils.showPerMissionDailog(Constant.FINALVALUE.PHONE_MESSAGE, WellcomeActivity.this);
                    return;
                }
            }
        });
    }

    /**
     * 查询手机号码
     */
    private void queryPhone() {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(this.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            DialogUtils.showPerMissionDailog(Constant.FINALVALUE.PHONE_MESSAGE, WellcomeActivity.this);
            return;
        }
        String imei = telephonyManager.getDeviceId();
        mainViewModel.getDtoLiveData(imei).observe(this, new Observer<BaseDto<QueryPhoneDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<QueryPhoneDto> queryPhoneDtoBaseDto) {
                //查询不到手机号码
                if (queryPhoneDtoBaseDto.getCode() == Constant.Server.SUCCESS_CODE && !EmptyUtils.isNotEmpty(queryPhoneDtoBaseDto.getData())) {
                    startActivity(new Intent(WellcomeActivity.this, ValidationMyPhoneActivity.class));
                } else {
                    startActivity(new Intent(WellcomeActivity.this, MainActivity.class));
                }
                finish();

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CcsApplication.removeActivity(this);
    }

}
